//
//  LMEditingPartnerViewController.m
//  LM
//
//  Created by Kien Tran on 2/5/13.
//  Copyright (c) 2013 Kien Tran. All rights reserved.
//

#import "LMEditingPartnerViewController.h"
#import "LMLocalPartner.h"
#import "LMLocalAccountManager.h"
#import "LMConstant.h"
#import "LMSexSelectionViewController.h"
#import "ActionSheetDatePicker.h"
@interface LMEditingPartnerViewController ()

@end

@implementation LMEditingPartnerViewController
@synthesize isEditingPartner = _isEditingPartner, phoneTextField = _phoneTextField, facebookTextField = _facebookTextField, firstDatingLabel = _firstDatingLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
   LMLocalPartner *partner = (LMLocalPartner *) self.localUser;
    
    if (!self.phoneTextField || [self.phoneTextField.text length] == 0) {
        self.phoneTextField.text = partner.phone ? partner.phone : @"";
    }
    
    
    if (!self.facebookTextField || [self.facebookTextField.text length] == 0) {
        self.facebookTextField.text = partner.facebook ? partner.facebook : @"";
    }
    
    if (!self.firstDatingLabel || [self.firstDatingLabel.text length] == 0) {
         self.firstDatingLabel.text = [partner getDatingFirstDateString];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)save:(id)sender {
       LMLocalPartner *partner = (LMLocalPartner *) self.localUser;
    
    partner.userName = self.nameTextField.text;
    partner.phone = self.phoneTextField.text;
    partner.facebook = self.facebookTextField.text;
    
    if (_isEditingPartner) {
        BOOL isEdited = [LMLocalAccountManager editPartnerWithNewPartner:partner];
        
        if (isEdited) {
            if (self.delegate) {
                [self.delegate editingLocalUseViewController:self saved:self.localUser];
            }
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        //Check limit number of partner.
        if ([LMLocalAccountManager canCreatePartner]) {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.dateFormat = kDATE_FORMAT;
            
            [LMLocalAccountManager createPartnerWithName:partner.userName sex:partner.sex birthday:partner.birthday phone:partner.phone facebook:partner.facebook datingFirstDate:partner.datingFirstDate];
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            [[[UIAlertView alloc] initWithTitle:kAlertTitle message:kLimitPartnerAlert delegate:nil cancelButtonTitle:kAlertOK otherButtonTitles:nil, nil] show];
        }
    }

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:kPushToSexSelectionViewController]) {
        LMSexSelectionViewController *sexController = [segue destinationViewController];
        sexController.localUser = self.localUser;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    if (indexPath.section == 0 ) {
        [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    } else if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
                [self.phoneTextField becomeFirstResponder];
                break;
            case 1:
                [self.facebookTextField becomeFirstResponder];
                break;
            case 2:
            {
                LMLocalPartner *partner = (LMLocalPartner *) self.localUser;
                
                NSDate *date = partner.datingFirstDate ? partner.datingFirstDate : [NSDate date];
                ActionSheetDatePicker *actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:date target:self action:@selector(firstDatingWasSelected:element:) origin:tableView];
                actionSheetPicker.hideCancel = YES;
                [actionSheetPicker showActionSheetPicker];
                break;
            }
            default:
                break;
        }
    }
}

- (void)firstDatingWasSelected:(NSDate *)selectedDate element:(id)element {
    LMLocalPartner *partner = (LMLocalPartner *) self.localUser;
    
    partner.datingFirstDate = selectedDate;
    
    //may have originated from textField or barButtonItem, use an IBOutlet instead of element
    
    self.firstDatingLabel.text = [partner getDatingFirstDateString];
}

- (void)viewDidUnload {
    [self setPhoneTextField:nil];
    [self setFacebookTextField:nil];
    [self setFirstDatingLabel:nil];
    [super viewDidUnload];
}
@end
